<?php

/**
 * ihelper
 * Helper plugin class for iwebform
 * @author Talha Buğra Bulut talhabulut@hotmail.com
 */
class ihelper {

    private $helpers = array();
    private $methods = array();

    public function __construct($helper_path=HELPER_PATH) {
        $this->load_helpers($helper_path);
    }

    private function load_helpers($helper_path) {
        $helpers = glob($helper_path . "/*.php");
        foreach ($helpers as $helper) {
            include_once $helper;

            $name = basename($helper, ".php");
            $className = "ihelper_" . $name;

            $obj = new $className();
            $this->helpers[$name] = $obj;

            foreach (get_class_methods($obj) as $method) {
                $this->methods[$method] = $name;
            }
        }
    }

    public function __call($method, $args) {
        if (!key_exists($method, $this->methods)) {
            throw new Exception("Call to undefined method : " . $method);
        }

        return call_user_func_array(array($this->helpers[$this->methods[$method]], $method), $args);
    }

}

?>